package com.sky.controller.user;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alibaba.fastjson.JSON;
import com.sky.result.Result;
import com.sky.service.user.UserOrderService;
import com.wechat.pay.java.core.Config;

import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;
import com.wechat.pay.java.service.refund.RefundService;
import com.wechat.pay.java.service.refund.model.AmountReq;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import com.wechat.pay.java.service.refund.model.Refund;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
@Slf4j
@RestController
public class WxPayController {
    @Autowired
    private Config config;
    @Autowired
    private NotificationConfig notificationConfig;
    @Autowired
    private UserOrderService userOrderService;
    @Autowired
    private UserOrderService orderService;

    @GetMapping("/paySuc")
    public String mockPaySuccess(String num){
        log.info("模拟用户支付成功 通知前端");
        userOrderService.paySuccess(num);
        return "OK";
    }



    @GetMapping("/wxpay")
    public Result wxPay() {
        NativePayService payService = new NativePayService.Builder().config(config).build();
        //微信文档中存在的构造参数
        PrepayRequest request = new PrepayRequest();
        //订单号
        request.setOutTradeNo("12345672346");
        request.setDescription("华为mate70pro+");
        request.setNotifyUrl("http://localhost:8080/nofify");
        request.setAppid("wx0ca99a203b1e9943");
        request.setMchid("1561414331");
        //是什么？
        Amount amount = new Amount();
        amount.setTotal(1);
        amount.setCurrency("CNY");
        request.setAmount(amount);

        PrepayResponse response = payService.prepay(request);
        String codeUrl = response.getCodeUrl();
        QrCodeUtil.generate(codeUrl,800,800,new File("G://pay.jpg"));
        return Result.success();
    }

    @GetMapping("/refund")
    public String refund(CreateRequest createRequest){
        RefundService refundService = new RefundService.Builder().config(config).build();
        //CreateRequest createRequest = new CreateRequest();
        //设置退款参数
   /*     createRequest.setOutTradeNo("12345672346");
        createRequest.setOutRefundNo("2233445566");*/
        AmountReq amountReq = new AmountReq();
        amountReq.setTotal(1L);
        amountReq.setRefund(1L);
        amountReq.setCurrency("CNY");
        createRequest.setAmount(amountReq);
        createRequest.setReason("取消订单退款");
        createRequest.setNotifyUrl("http://10bbcfd3.r12.cpolar.top/wxpay/refund/notify");
        //发起退款
        Refund refund = refundService.create(createRequest);
        return JSON.toJSONString(refund);
    }





}
